package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerManager;

public class CustomerManagerImpl implements CustomerManager{
	//线程安全list
	private List<Customer> customers = Collections.synchronizedList(new ArrayList<Customer>());
	
	public CustomerManagerImpl() {
		Customer c1 = new Customer("1111", "Kelvin", true, 333.45, new Date());
		Customer c2 = new Customer("2222", "Jack", true, 777.25, new Date());
		Customer c3 = new Customer(UUID.randomUUID().toString(), "Lisa", false, 555.45, new Date());
		customers.add(c1);
		customers.add(c2);
		customers.add(c3);
	}

	@Override
	public Customer addCustomer(Customer customer) {
		if(null==customer){
			return null;
		}
		customer.setId(UUID.randomUUID().toString());
		customers.add(customer);
		return customer;
	}

	@Override
	public Customer updateCustomer(Customer customer) {
		if(null==customer){
			return null;
		}
		if(!customers.contains(customer)){
			return null;
		}
		
		for(Customer c:customers){
			if(c.getId().equals(customer.getId())){
				c.setName(customer.getName());
				c.setBirth(customer.getBirth());
				c.setName(customer.getName());
				c.setSalary(customer.getSalary());
				c.setSex(customer.isSex());
			}
		}
		return customer;
	}

	@Override
	public Customer deleteCustomerById(String id) {
		if(null==id){
			return null;
		}
		Customer customer = new Customer(id);
		if(!customers.contains(customer)){
			return null;
		}
		int index = customers.indexOf(customer);
		customer = customers.get(index);
		customers.remove(index);
		return customer;
	}

	@Override
	public List<Customer> findAll() {
		return customers;
	}

	@Override
	public Customer findCustomerById(String id) {
		if(null==id){
			return null;
		}
		Customer customer = new Customer(id);
		if(!customers.contains(customer)){
			return null;
		}
		return customers.get(customers.indexOf(customer));
	}

}
